<?php
/**
 * Created by PhpStorm.
 * User: yangliqiang
 * Date: 2018/8/22
 * Time: 21:31
 */

namespace app\api\models;


use app\api\core\wechat\wxAction;
use yii\base\Model;
use Cm;

class MenuModel extends Model
{

    public function  actionFetLit()
    {
        $appid = Cm::$req->post('appid', ['reg'=>'/^[0-9A-Za-z]{1,}/', 'title'=>'微信公总号APPID']);

        $wechat = new wxAction($appid);

        $result = $wechat->getSelfMenu();

        if ($result && is_array($result)) {
            Cm::$res->susess('success',$result);
        } else {
            Cm::$res->error(!empty($wechat->error) ? $wechat->error : $wechat->errMsg);
        }
    }
    public function actionAdd()
    {
        $appid = Cm::$req->post('appid', ['reg'=>'/^[0-9A-Za-z]{1,}/', 'title'=>'微信公总号APPID']);
        $menus = Cm::$req->post('menus', ['reg' => '/[^,:{}\\[\\]0-9.\-+Eaeflnr-u \n\r\t]/', 'title' => '菜单']);

        $wechat = new wxAction($appid);

        $data = json_decode($menus, true);

        if (!is_array($data) || !empty($data)) {
            Cm::$res->error('菜单json格式不正确，请仔细阅读文章说明');
        }
        $result = $wechat->createMenu($data);

        if ($result) {
            Cm::$res->susess('发布菜单成功');
        } else {
            Cm::$res->error(!empty($wechat->error) ? $wechat->error : $wechat->errMsg);
        }
    }
}